package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public void doTime() {
    }

    @Before("doTime()")
    public void beforeMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();

        String args = Arrays.toString(joinPoint.getArgs());

        System.out.println(name + "方法开始执行,参数为:" + args);

    }

    @After("doTime()")
    public void afterMethod(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();

        System.out.println(name + "方法执行完毕");

    }

    @AfterReturning(value = "doTime()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        String name = joinPoint.getSignature().getName();

        System.out.println(name + "方法正常返回,结果为:" + result);

    }

    @AfterThrowing(value = "doTime()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
        String name = joinPoint.getSignature().getName();

        System.out.println(name + "方法异常返回,异常为:" + ex.getMessage());

    }

    @Around("doTime()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            System.out.println("方法执行开始...");
            result = joinPoint.proceed();
            System.out.println("方法执行完毕...");
            System.out.println("方法返回结果为:" + result);

        } catch (Throwable ex) {
            System.out.println("方法异常返回,异常为:" + ex.getMessage());
        } finally {
            System.out.println("方法执行完毕...");
        }
        return result;
    }

}





































































